package com.xx.springboot.DataStructure.LeetCode.LinkedList;

import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;

/**
 * Created by xiaoxiunique on 2018-8-10.
 * 输入：1->2->4, 1->3->4
 * 输出：1->1->2->3->4->4
 */
public class mergeTwoLists {
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        ListNode dummyHead = new ListNode(0);
        ListNode newNode = dummyHead;

        while (l1 != null && l2 != null){
            if(l1.val > l2.val){
                newNode.next = new ListNode(l2.val);
                l2 = l2.next;
            }
            else {
                newNode.next = new ListNode(l1.val);
                l1 = l1.next;
            }
            newNode = newNode.next;
        }

        newNode.next = l1 == null ? l2 : l1;
        return dummyHead.next;
    }

    public static void main(String[] args) {

        int[] arr1 = {11, 21, 31, 41, 51};
        int[] arr2 = {2, 4, 6};

        ListNode node1 = new ListNode(arr1);
        ListNode node2 = new ListNode(arr2);
        System.out.println( (new mergeTwoLists()).mergeTwoLists(node1, node2) );
        System.out.println();
    }

}
